package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.GoodsStore;
import cn.wolfcode.car.business.domain.StockBill;
import cn.wolfcode.car.business.domain.StockBillItem;
import cn.wolfcode.car.business.mapper.GoodsStoreMapper;
import cn.wolfcode.car.business.mapper.StockBillItemMapper;
import cn.wolfcode.car.business.mapper.StockBillMapper;
import cn.wolfcode.car.business.mapper.StoreMapper;
import cn.wolfcode.car.business.query.StockBillQuery;
import cn.wolfcode.car.business.service.IStockBillService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.apache.regexp.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StockBillServiceImpl implements IStockBillService {

    @Autowired
    private StockBillMapper stockBillMapper;
    @Autowired
    private StockBillItemMapper stockBillItemMapper;
    @Autowired
    private GoodsStoreMapper goodsStoreMapper;
    @Autowired
    private StoreMapper storeMapper;

    @Override
    public TablePageInfo<StockBill> query(StockBillQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StockBill>(stockBillMapper.selectForList(qo));
    }

    @Override
    public void save(StockBill stockBill) {
        stockBill.setBusiDate(new Date());
        stockBill.setOperateDate(new Date());
        stockBill.setOperatorId(ShiroUtils.getUserId());
        // 新增操作
        stockBillMapper.insert(stockBill);

        // 将出入库单内的货品数量同步更新到仓库内存
        if (stockBill.getType() == 1) {
            List<StockBillItem> billItems = stockBillItemMapper.selectByBillId(0L);
            for (StockBillItem billItem : billItems) {
                GoodsStore goodsStore = goodsStoreMapper.selectByGoodsIdAndStoreId(stockBill.getStoreId(), billItem.getGoodsId());
                if (billItem.getAmounts() > goodsStore.getAmounts()) {
                    throw new BusinessException("库存不足!");
                }
                goodsStore.setAmounts(goodsStore.getAmounts() - billItem.getAmounts());
                if (goodsStore.getAmounts() == 0 ){
                    goodsStoreMapper.deleteByPrimaryKey(goodsStore.getId());
                    stockBillItemMapper.deleteByGoodsId(goodsStore.getGoodsId());
                }else {
                    goodsStoreMapper.updateByPrimaryKey(goodsStore);
                }
            }
            return;
        }
        List<StockBillItem> billItems = stockBillItemMapper.selectByBillId(0L);
        for (StockBillItem billItem : billItems) {
            GoodsStore goodsStore = goodsStoreMapper.selectByGoodsIdAndStoreId(stockBill.getStoreId(), billItem.getGoodsId());
            if (goodsStore != null) {
                goodsStore.setAmounts(billItem.getAmounts() + goodsStore.getAmounts());
                goodsStoreMapper.updateByPrimaryKey(goodsStore);
            }else {
                GoodsStore store = new GoodsStore();
                store.setGoodsId(billItem.getGoodsId());
                store.setStoreId(stockBill.getStoreId());
                store.setAmounts(billItem.getAmounts());
                goodsStoreMapper.insert(store);
            }
        }
        // 新增 stockBill 将新增库单的id更新到 stockBillItem 内 billId为0的物品
        stockBillItemMapper.updateBillId(stockBill.getId());
    }

    @Override
    public StockBill get(Long id) {
        return stockBillMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StockBill> list() {
        return stockBillMapper.selectAll();
    }

    @Override
    public void update(StockBill stockBill) {
        stockBillMapper.updateByPrimaryKey(stockBill);
    }

    @Override
    @Transactional
    public void cancelBill(Long id) {
        StockBill stockBill = this.get(id);
        // 获取 出入库单 操作的仓库id
        Long storeId = stockBill.getStoreId();

        // 查询对应的出入库明细
        List<StockBillItem> stockBillItems = stockBillItemMapper.selectByBillId(id);

        // 作废前，需要恢复出入库单中对应物品在仓库的原数量
        // 如果是入库单
        if (StockBill.STOCK_IN.equals(stockBill.getType())) {
            for (StockBillItem stockBillItem : stockBillItems) {
                // 获取入库单的相应物品数量
                Integer billItemsAmounts = stockBillItem.getAmounts();

                // 获取该物品在仓库中的数量
                GoodsStore goodsStore = goodsStoreMapper.selectByGoodsIdAndStoreId(storeId, stockBillItem.getGoodsId());
                if (goodsStore == null) {
                    throw new BusinessException("该仓库中现有库存不足以作废回滚库存！作废失败！");
                }
                Integer storeAmounts = goodsStore.getAmounts();

                // 如果当初入库数量 大于 现有库存量 则无法恢复仓库的原数量 报错
                if (billItemsAmounts > storeAmounts) {
                    throw new BusinessException("该仓库中现有库存不足以作废回滚库存！作废失败！");
                } else {
                    // 仓库足够库存   进行回滚
                    goodsStoreMapper.changeAmounts(goodsStore.getId(), goodsStore.getAmounts() - billItemsAmounts);
                }
            }
        } else {
            // 如果是出库单
            for (StockBillItem stockBillItem : stockBillItems) {
                // 获取入库单的相应物品数量
                Integer billItemsAmounts = stockBillItem.getAmounts();

                // 回滚仓库数量
                GoodsStore goodsStore = goodsStoreMapper.selectByGoodsIdAndStoreId(storeId, stockBillItem.getGoodsId());

                if (goodsStore == null) {
                    GoodsStore goodsStore1 = new GoodsStore();
                    goodsStore1.setStoreId(storeId);
                    goodsStore1.setGoodsId(stockBillItem.getGoodsId());
                    goodsStore1.setAmounts(stockBillItem.getAmounts());
                    goodsStoreMapper.insert(goodsStore1);
                }else {
                    goodsStoreMapper.changeAmounts(goodsStore.getId(), goodsStore.getAmounts() + billItemsAmounts);
                }
            }
        }

        StockBillItem stockBillItem = stockBillItems.get(0);
        // 出入库明细 状态修改
        stockBillItemMapper.changeState(stockBillItem.getId(), StockBillItem.STOCK_BILL_ITEM_STATE_CANCEL);

        // 出入库单作废 状态修改
        stockBillMapper.cancelBill(id, StockBill.STOCK_BILL_CANCEL);
    }

    @Override
    public List<StockBill> checkBill(Long id) {
        List<StockBill> list = stockBillMapper.checkBill(id);
        return list;
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            stockBillMapper.deleteByPrimaryKey(dictId);
        }
    }


}
